﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for CKVN.
    /// </summary>
    public class VOTE_1
    {

        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();
        public VOTE_1(SMS_MO MO)
        {
            WMO = MO;
        }

        public VOTE_1()
        {
        }
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string strReturnMsg = GetReturnMsg(msg);

            WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
            WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
            WMT.Insert();
            return;
        }
        private string GetReturnMsg(string[] arr)
        {
            DateTime ngayketthuc = new DateTime(2012, 05, 07, 22, 00, 00);
            //// DateTime  ngayketthucss = new DateTime(2012,05,07);
            DateTime ngayhientai = DateTime.Now;
            int kq = DateTime.Compare(ngayhientai, ngayketthuc);           

            string msgBody = string.Empty;

            if ((WMO.ServiceID.Replace("04", "") == "6772") || (WMO.UserID == "84978925568"))
            {
                if (kq >= 0)
                    msgBody = "Chuong trinh binh chon TUU's Hidden Beauty 2012 ket thuc luc 22h00 ngay 07-05-2012. Chuong trinh duoc to chuc boi svcongdoan.com”";
                else
                {
                    msgBody = "Ban da binh chon 4 phieu cho SBD " + arr[0] + ". Chuong trinh duoc to chuc boi svcongdoan.com”";
                    InsertVote(arr[0], "", 4, WMO.UserID, DateTime.Now);
                }

            }
            else if (WMO.ServiceID.Replace("04", "") == "6572" || (WMO.UserID == "84902222776") || (WMO.UserID == "84978925568"))
            {
                if (kq >= 0)
                    msgBody = "Chuong trinh binh chon TUU's Hidden Beauty 2012 ket thuc luc 22h00 ngay 07-05-2012. Chuong trinh duoc to chuc boi svcongdoan.com”";
                else
                {
                    msgBody = "Ban da binh chon 1 phieu cho SBD " + arr[0] + ". Chuong trinh duoc to chuc boi svcongdoan.com”";
                    InsertVote(arr[0], "", 1, WMO.UserID, DateTime.Now);
                }
            }
            else
            {
                msgBody = "Loi cu phap,de binh chon soan tin: TUU <SoBaoDanh> gui 6572 hoặc TUU <SoBaoDanh> gui 6772";
            }
            return msgBody;
        }
        private int InsertVote(string sbd, string hoten, int bc, string sdt, DateTime d)
        {
            string strSQLInsert = "";
            string strSQLconnec = "server=210.86.230.201;uid=sa;pwd=vittelecom!@#;database=VIT_SEL;"; //ConfigSettings.ReadSetting("CONNECTION_STRING_7x62");
            strSQLInsert = "INSERT INTO vote_2(sbd,hoten, bc,sdt,date)";
            strSQLInsert += " VALUES(@sbd,@hoten, @bc,@sdt,@date)";
            SqlConnection cnn = new SqlConnection(strSQLconnec);
            cnn.Open();

            SqlCommand cmd;
            cmd = new SqlCommand(strSQLInsert, cnn);
            cmd.Parameters.Clear();

            cmd.Parameters.AddWithValue("@sbd", sbd);
            cmd.Parameters.AddWithValue("@hoten", hoten);
            cmd.Parameters.AddWithValue("@bc", bc);
            cmd.Parameters.AddWithValue("@sdt", sdt);
            cmd.Parameters.AddWithValue("@date", d);

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            cnn.Close();
            return 1;
        }
    }
}